global data /bplimext/projects/p064_MathildeMunoz/initial_dataset
global outputs /bplimext/projects/p064_MathildeMunoz/work_area

global list 33120 33200 41200 42110 42120 42130 42210 42220 42910 42990 ///
43110 43120 43130 43210 43221 43222 43290 43310 43320 43330 43340 43390 43910 43992 49410 78200 

global nonpostable 49320 4711 47112 47191 47210 47220 47230 47240 47250 47260 47291 47292 ///
55111 55112 55113 55114 55115 55121 55122 56101 56102 56103 56104 56210 56290 56301 56302 ///
56303 56304 75000 96021 96022

*****************************************************
* Robustness using "non-postable" sectors as controls
******************************************************

use "$data/PE064_CBHP_A_YFRM_20062017_JUN19_ROSTO_V01.dta", clear
merge 1:1 ano tina using "$data/PE064_CBHP_A_YFRM_20062017_JUN19_MG_V01.dta"
drop _m
merge 1:1 ano tina using "$data/PE064_CBHP_A_YFRM_20062017_JUN19_PESSOAL_V01.dta"
drop _m
g export_service_EU=0
replace export_service_EU=1 if MG005!=0
bys ano: tab export_service_EU
bys ano: tab exporta
g export_sales_EU=0
replace export_sales_EU=1 if MG004!=0
bys ano: tab export_sales_EU



g postable=0
foreach y in $list {
replace postable=1 if cae3==`y'
}

g nonpostable=0
foreach y in $nonpostable {
replace nonpostable=1 if cae3==`y'
}

g flag=1
egen tot=sum(flag), by(ano cae3)

merge 1:1 ano tina using "$data/PE064_CBHP_A_YFRM_20062017_JUN19_CONTAS_V01.dta"
drop _m

egen panel_export=sum(export_service_EU), by(tina
g wage_cap=D029/E012
drop if postable==0 & nonpostable==0

bysort tina (ano):gen cum=sum(export_service_EU)

g event=ano if cum==1 & export_service_EU==1
egen event_d=min(event), by(tina)
replace event_d=0 if missing(event_d)
g treated=0
replace treated=1 if panel_export>=1

g time=ano-event_d

replace time=0 if treated==0 & panel_export==0

g timed=time+11 if treated==1
replace timed=10 if treated==0

forval y=0/22{
g coeff_`y'=0
replace coeff_`y'=1 if timed==`y'
}

g alert=0
replace alert=1 if time>0 & export_service_EU==0
g zero=1
label var zero "-1"

forval y=0/22{
local j=`y'-11
label var coeff_`y' "`j'"
}

preserve 

g freq=1
collapse (sum) export_service_EU MG005 MG004 export_sales_EU freq D001, by(postable nonpostable ano cae3)
g s_export_services=export_service_EU/freq
g s_export_goods=export_sales_EU/freq
egen tot=sum(D001), by(cae3 ano)
g a_export_services=MG005/tot
g a_export_goods=MG004/tot
restore 

egen cluster=group(cae3 distrito)

drop if treated==1 & postable==0


xtset tina ano
g logE001=log(E001)
g logemp=log(E001)
g loghours=log(E012)
g logwage=log(wage_cap)
g logwagebill=log(D029)
g logca=log(D001)
g logtemp=log(E010)
g logEBT=log(D086)
g logparttime=log(E007)
g loginternal=log(MG002)
g logassets=log(B001)
g logservices=log(MG011)
g logturnoverinternal=log(MG001+MG002)
g logestabfor=log(numestabest)
g logtangible=log(B012)
g logcash=log(B049)
g loghourstemp=log(E018)
g loghourspart=log(E017)
g logssc=log(DL013)
g logtax=log(D060)
g logdebt=log(B081)


*drop firms that export to other EU market (no posting)
g export_other=0
replace export_other=1 if MG008>0
egen panel_export_other=sum(export_other), by(tina)
g alt_treat=0
replace alt_treat=1 if panel_export_other>=1
drop if alt_treat==1
drop cluster
egen cluster1=group(cae3 distrito)
egen cluster2=group(cae3 ano)
egen cluster3=group(cae3 dimcomissao)
*binning coefficients
g coeff_up=0
replace coeff_up=1 if treated==1 & time>6
g coeff_bottom=0
replace coeff_bottom=1 if treated==1 & time<-4

label var coeff_up ">6"
drop if event_d<2010 & treated==1
drop if event_d>2015 & treated==1

g coeff_up2=0
replace coeff_up2=1 if time>4 & treated==1
label var coeff_up2 ">4"

global depvar logassets E001 logemp loghours logwage logwagebill logca logEBT logparttime loginternal
label var coeff_bottom "-4<"

   egen median=median(E001), by(tina)
   drop if median<3
   drop if median==.
   
   g export_servicedum=export_service_EU 
   replace export_servicedum=0 if ano==event_d
   
   
g alt=0
replace alt=1 if treated==0 & postable==1
   
   
 
foreach x in  logEBT logwage logca logemp loghours logwagebill{
forval y=1/1{
reghdfe `x' coeff_bottom coeff_7-coeff_9 zero coeff_11-coeff_15 coeff_up2 , absorb( i.tina i.ano i.distrito i.ano##i.distrito) cluster(cluster`y')


coefplot, keep( coeff_7 coeff_8 coeff_9 zero coeff_11 coeff_12 coeff_13 coeff_14 coeff_15  ) omit ///
vertical  graphregion(fcolor(white) lcolor(white)) ciopts(recast(rcap) lwidth(medthick))  yline(0, lpattern(dash) lcolor(gs10)) ylabel(-.2 (.2) .6) ///
xline(4.5, lcolor(red)) scale(1.2)

graph export "$outputs/ES/Baseline_Export5/Figures/`x'_full_c`y'_cut_nonpostable.png", replace
}
}  
   
    
foreach x in  logEBT logwage logca logemp loghours{
forval y=1/1{
reghdfe `x' coeff_bottom coeff_7-coeff_9 zero coeff_11-coeff_15 coeff_up2 if alt==0, absorb( i.tina i.ano i.distrito i.ano##i.distrito) cluster(cluster`y')


coefplot, keep( coeff_7 coeff_8 coeff_9 zero coeff_11 coeff_12 coeff_13 coeff_14 coeff_15  ) omit ///
vertical  graphregion(fcolor(white) lcolor(white)) ciopts(recast(rcap) lwidth(medthick))  yline(0, lpattern(dash) lcolor(gs10)) ylabel(-.2 (.2) .6) ///
xline(4.5, lcolor(red)) scale(1.2)

graph export "$outputs/ES/Baseline_Export5/Figures/`x'_full_c`y'_cut_nonpostable_ot.png", replace
}
}  
   
   
   
   


